package com.qianfeng.openplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.webmaster.client.CacheClient;
import com.qianfeng.openplatform.webmaster.entry.LayuiDataTableSource;
import com.qianfeng.openplatform.webmaster.entry.OpenApiMaster;
import com.qianfeng.openplatform.webmaster.mapper.ApiSystemparamMapper;
import com.qianfeng.openplatform.webmaster.pojo.ApiSystemparam;
import com.qianfeng.openplatform.webmaster.pojo.ApiSystemparamExample;
import com.qianfeng.openplatform.webmaster.service.SysParameterService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/25 10:39
 */
@Service
@Transactional
public class SysParameterServiceImpl implements SysParameterService {
    @Autowired
    private ApiSystemparamMapper apiSystemparamMapper;
    @Autowired
    private CacheClient cacheClient;

    @Override
    public LayuiDataTableSource getSysParameterPage(Integer page, Integer limit, String name, Integer state) {
        PageHelper.startPage(page, limit);
        ApiSystemparamExample example = new ApiSystemparamExample();
        ApiSystemparamExample.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            c.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(state)) { //state值不为空的时候执行
        c.andStateEqualTo(state.shortValue());
    }
        List<ApiSystemparam> systemparamList = apiSystemparamMapper.selectByExample(example);
        PageInfo<ApiSystemparam> info = new PageInfo<>(systemparamList);
        LayuiDataTableSource source = new LayuiDataTableSource();
        source.setCount(info.getTotal());;
        source.setData(info.getList());
        return source;
    }

    @Override
    //添加
    public OpenApiMaster addSysParameter(ApiSystemparam apiSystemparam) {
        int i = apiSystemparamMapper.insert(apiSystemparam);
        if (i>0){
            try {
                cacheClient.sAdd(RedisKeyPrefix.WEB_MASTER_API_MAPPING_PARAMS_KEY,apiSystemparam.getName(),-1l);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("添加失败");
    }

    @Override
    //修改
    public OpenApiMaster updateSysParamter(ApiSystemparam apiSystemparam) {
        int i = apiSystemparamMapper.updateByPrimaryKey(apiSystemparam);
        if (i>0){
            try {
            if (apiSystemparam.getState()==0){

                    System.out.println("SysParameterServiceImpl-add:------->"+apiSystemparam.getName());
                    cacheClient.sRemove(RedisKeyPrefix.WEB_MASTER_API_MAPPING_PARAMS_KEY,apiSystemparam.getName());
            }else{
                cacheClient.sAdd(RedisKeyPrefix.WEB_MASTER_API_MAPPING_PARAMS_KEY,apiSystemparam.getName(),-1l);
            }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("添加失败");
    }

    @Override
    //删除 mysql redis ids
    public OpenApiMaster delSysParameter(Integer[] ids) {
        List<ApiSystemparam> systemparamList = new ArrayList<>();
        for (Integer id : ids) {
            ApiSystemparam systemparam = apiSystemparamMapper.selectByPrimaryKey(id);
            systemparamList.add(systemparam);
        }
        int type=0;
        int i =0;
        for (Integer id : ids) {
             i += apiSystemparamMapper.deleteByPrimaryKey(id);
            type++;
        }
        if (type==i){
            for (ApiSystemparam sys: systemparamList) {
                try {
                    System.out.println("SysParameterServiceImpl-87"+sys.getName());
                    cacheClient.sRemove(RedisKeyPrefix.WEB_MASTER_API_MAPPING_PARAMS_KEY,sys.getName());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }



            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("删除失败");
    }


}
